package ynu.edu.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ynu.edu.common.controller.LogicController;
import ynu.edu.dao.ICustomerDao;
import ynu.edu.dto.*;
import ynu.edu.entity.*;
import ynu.edu.service.CustomerService;
import ynu.edu.utils.Result;
import ynu.edu.utils.TokenUtils;
import ynu.edu.utils.inna.AuthPermission;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "顾客实体的控制器")
@RestController
@RequestMapping("/customer")
public class CustomerController extends LogicController<CustomerService, ICustomerDao, Customer,Long> {
    public CustomerController(@Autowired CustomerService ls) {
        super(ls);
    }
    @Resource
    private  ModelMapper modelMapper;

    @AuthPermission(role = User.TYPE_CUSTOMER)
    @ApiOperation("顾客获取在门店下没有支付的订单")
    @GetMapping("/getOrderByShop/{shopId}")
    public Result getOrderByShop(@PathVariable("shopId") Long shopId){
        User user = TokenUtils.getCurrentUser();
        Order order = getService().getUnpaidOrderByShop(user,shopId);
        return getResult(modelMapper,order);
    }

    @AuthPermission(role = User.TYPE_CUSTOMER)
    @ApiOperation("顾客下单")
    @PostMapping("/placeOrder")
    @Transactional
    public Result placeOrder(@RequestBody OrderLineItemDTO orderLineItem){
        User user = TokenUtils.getCurrentUser();
        Order order = getService().placeOrder(user,orderLineItem);
        return getResult(modelMapper,order);
    }

    @AuthPermission(role = User.TYPE_CUSTOMER)
    @ApiOperation("支付订单")
    @PostMapping("/payOrder/{orderId}")
    @Transactional
    public Result payOrder(@PathVariable("orderId") Long orderId){
        User user = TokenUtils.getCurrentUser();
        Order order = getService().payOrder(user, orderId);
        return getResult(modelMapper,order);
    }

    @AuthPermission(role = User.TYPE_CUSTOMER)
    @ApiOperation("取消订单")
    @PostMapping("/cancelOrder/{orderId}")
    @Transactional
    public Result cancelOrder(@PathVariable("orderId") Long orderId){
        User user = TokenUtils.getCurrentUser();
        Order order = getService().cancelOrder(user, orderId);
        return getResult(modelMapper,order);
    }

    @AuthPermission(role = User.TYPE_CUSTOMER)
    @ApiOperation("获取全部订单")
    @GetMapping("/getMyOrders")
    public Result getMyOrders(){
        User user = TokenUtils.getCurrentUser();
        List<Order> orders = getService().getOrders(user);
        List<OrderDTO> orderDTOS =new ArrayList<>();
        for (Order order : orders) {
            List<LineItemDTO> lineItemDTOS = order.getLineItems().stream().map(
                    lineItem -> modelMapper.map(lineItem, LineItemDTO.class)).toList();
            OrderDTO orderDTO = modelMapper.map(order, OrderDTO.class);
            orderDTO.setLineItemDTOS(lineItemDTOS);
            orderDTOS.add(orderDTO);
        }
        return Result.success(orderDTOS);
    }


    public static Result getResult(ModelMapper modelMapper,Order order) {
        if (order == null) return Result.success();
        List<LineItemDTO> lineItemDTOS = order.getLineItems().stream().map(
                lineItem -> modelMapper.map(lineItem, LineItemDTO.class)).toList();
        OrderDTO orderDTO = modelMapper.map(order, OrderDTO.class);
        orderDTO.setLineItemDTOS(lineItemDTOS);
        return  Result.success(orderDTO);
    }
}
